/*---------------------------------------------------------------------------*\
|* This file is part of the koala project with MIT License.                  *|
|* Copyright (c) James <zhuguangxiang@gmail.com>                             *|
\*--------------------------------------------------------------------------**/

#include "log.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef NOLOG

static int log_level = LOG_DEBUG;
static char *log_str[] = { "debug", "info", "warn", "error" };
static char *log_color[] = { "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m" };

void log_set_level(int level)
{
    log_level = level;
}

void log_log(int level, char *file, int line, char *fmt, ...)
{
    if (level < log_level) return;

    fprintf(stdout, "%s%-5s\x1b[0m %s:%d: ", log_color[level], log_str[level],
            file, line);

    va_list ap;
    va_start(ap, fmt);
    fprintf(stdout, fmt, ap);
    va_end(ap);

    fprintf(stdout, "\n");
    fflush(stdout);
}

#endif

#ifdef __cplusplus
}
#endif
